// pages/waitSubmit/index.js
const app = getApp()
wx.cloud.init({
  traceUser: true
})
const db = wx.cloud.database()
const savedvoice = db.collection('savedvoice')
import { dateFormat } from '../../utils/dateFormat.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoading: true,
    saveList: [],
    showComfire: false,
    operateId: '',
    hasContent: false
  },

  getData(){
    savedvoice.where({
      _openid: app.user._openid
    }).orderBy('createDate', 'desc').get().then(res => {
      let list = res.data
      list.forEach((item, index) => {
        let content = item.voiceContent.replace(/<\/?.+?>/g, "")
        list[index].voiceContentAll = item.voiceContent
        list[index].voiceContent = content.substring(0, 30) + (content.length > 30 ? '...' : '')
        list[index].submitDate = dateFormat(new Date(item.submitDate), 'yyyy-MM-dd hh:mm')
      })
      this.setData({
        saveList: list,
        isLoading: false
      })
      if (this.data.saveList.length == 0) {
        this.setData({
          hasContent: true
        })
      }
    })
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target,
      operateId: e.currentTarget.dataset.id
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null,
      showComfire: false
    })
  },
  comfireDelete(){
    if(!this.data.showComfire){
      this.setData({
        showComfire: true
      })
    }else{
      wx.showLoading({
        title: '删除中',
        mask: true,
      })
      wx.cloud.callFunction({
        name: 'deleteSavedVoice',
        data: {
          voiceId: this.data.operateId
        }
      }).then(res => {
        if (res.result == 1) {
          this.setData({
            modalName: null,
            showComfire: false
          })
          this.data.saveList = this.data.saveList.filter(item => {
            return item._id != this.data.operateId
          })
          this.setData({
            operateId: '',
            saveList: this.data.saveList
          })
          // this.getVoiceData(this.data.currentPage)
          app.sumCount = {}
          wx.showToast({
            title: '删除成功',
            icon: 'success',
            duration: 1000
          })
        }
      }).catch(err => {
        console.log(err)
      })
    }
  },
  saveToSubmit() {
    wx.showLoading({
      title: '正在上传',
      mask: true,
    })
    wx.cloud.callFunction({
      name: 'saveToSubmit',
      data: {
        voiceId: this.data.operateId
      }
    }).then(res => {
      this.setData({
        modalName: null
      })
      this.data.saveList = this.data.saveList.filter(item => {
        return item._id != this.data.operateId
      })
      this.setData({
        operateId: '',
        saveList: this.data.saveList
      })
      // this.triggerEvent('setRefresh', true)
      app.sumCount = {}
      wx.showToast({
        title: '发布成功',
        icon: 'success',
        duration: 1000
      })
    })
  },
  reEdit(){
    wx.redirectTo({
      url: '/pages/index/index?component=submit&saveId=' + this.data.operateId,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})